{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.5 \u5728\u7c7b\u4e2d\u5c01\u88c5\u5c5e\u6027\u540d\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c01\u88c5\u7c7b\u7684\u5b9e\u4f8b\u4e0a\u9762\u7684\u201c\u79c1\u6709\u201d\u6570\u636e\uff0c\u4f46\u662fPython\u8bed\u8a00\u5e76\u6ca1\u6709\u8bbf\u95ee\u63a7\u5236\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u7a0b\u5e8f\u5458\u4e0d\u53bb\u4f9d\u8d56\u8bed\u8a00\u7279\u6027\u53bb\u5c01\u88c5\u6570\u636e\uff0c\u800c\u662f\u901a\u8fc7\u9075\u5faa\u4e00\u5b9a\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u547d\u540d\u89c4\u7ea6\u6765\u8fbe\u5230\u8fd9\u4e2a\u6548\u679c\u3002\n\u7b2c\u4e00\u4e2a\u7ea6\u5b9a\u662f\u4efb\u4f55\u4ee5\u5355\u4e0b\u5212\u7ebf_\u5f00\u5934\u7684\u540d\u5b57\u90fd\u5e94\u8be5\u662f\u5185\u90e8\u5b9e\u73b0\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class A:\n    def __init__(self):\n        self._internal = 0 # An internal attribute\n        self.public = 1 # A public attribute\n\n    def public_method(self):\n        '''\n        A public method\n        '''\n        pass\n\n    def _internal_method(self):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u5e76\u4e0d\u4f1a\u771f\u7684\u963b\u6b62\u522b\u4eba\u8bbf\u95ee\u5185\u90e8\u540d\u79f0\u3002\u4f46\u662f\u5982\u679c\u4f60\u8fd9\u4e48\u505a\u80af\u5b9a\u662f\u4e0d\u597d\u7684\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8106\u5f31\u7684\u4ee3\u7801\u3002\n\u540c\u65f6\u8fd8\u8981\u6ce8\u610f\u5230\uff0c\u4f7f\u7528\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u7ea6\u5b9a\u540c\u6837\u9002\u7528\u4e8e\u6a21\u5757\u540d\u548c\u6a21\u5757\u7ea7\u522b\u51fd\u6570\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u770b\u5230\u67d0\u4e2a\u6a21\u5757\u540d\u4ee5\u5355\u4e0b\u5212\u7ebf\u5f00\u5934(\u6bd4\u5982_socket)\uff0c\u90a3\u5b83\u5c31\u662f\u5185\u90e8\u5b9e\u73b0\u3002\n\u7c7b\u4f3c\u7684\uff0c\u6a21\u5757\u7ea7\u522b\u51fd\u6570\u6bd4\u5982 sys._getframe() \u5728\u4f7f\u7528\u7684\u65f6\u5019\u5c31\u5f97\u52a0\u500d\u5c0f\u5fc3\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u53ef\u80fd\u4f1a\u9047\u5230\u5728\u7c7b\u5b9a\u4e49\u4e2d\u4f7f\u7528\u4e24\u4e2a\u4e0b\u5212\u7ebf(__)\u5f00\u5934\u7684\u547d\u540d\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class B:\n    def __init__(self):\n        self.__private = 0\n\n    def __private_method(self):\n        pass\n\n    def public_method(self):\n        pass\n        self.__private_method()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u53cc\u4e0b\u5212\u7ebf\u5f00\u59cb\u4f1a\u5bfc\u81f4\u8bbf\u95ee\u540d\u79f0\u53d8\u6210\u5176\u4ed6\u5f62\u5f0f\u3002\n\u6bd4\u5982\uff0c\u5728\u524d\u9762\u7684\u7c7bB\u4e2d\uff0c\u79c1\u6709\u5c5e\u6027\u4f1a\u88ab\u5206\u522b\u91cd\u547d\u540d\u4e3a _B__private \u548c _B__private_method \u3002\n\u8fd9\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u95ee\u8fd9\u6837\u91cd\u547d\u540d\u7684\u76ee\u7684\u662f\u4ec0\u4e48\uff0c\u7b54\u6848\u5c31\u662f\u7ee7\u627f\u2014\u2014\u8fd9\u79cd\u5c5e\u6027\u901a\u8fc7\u7ee7\u627f\u662f\u65e0\u6cd5\u88ab\u8986\u76d6\u7684\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class C(B):\n    def __init__(self):\n        super().__init__()\n        self.__private = 1 # Does not override B.__private\n\n    # Does not override B.__private_method()\n    def __private_method(self):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\uff0c\u79c1\u6709\u540d\u79f0 __private \u548c __private_method\n\u88ab\u91cd\u547d\u540d\u4e3a _C__private \u548c _C__private_method \uff0c\u8fd9\u4e2a\u8ddf\u7236\u7c7bB\u4e2d\u7684\u540d\u79f0\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u63d0\u5230\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u7f16\u7801\u7ea6\u5b9a(\u5355\u4e0b\u5212\u7ebf\u548c\u53cc\u4e0b\u5212\u7ebf)\u6765\u547d\u540d\u79c1\u6709\u5c5e\u6027\uff0c\u90a3\u4e48\u95ee\u9898\u5c31\u6765\u4e86\uff1a\u5230\u5e95\u54ea\u79cd\u65b9\u5f0f\u597d\u5462\uff1f\n\u5927\u591a\u6570\u800c\u8a00\uff0c\u4f60\u5e94\u8be5\u8ba9\u4f60\u7684\u975e\u516c\u5171\u540d\u79f0\u4ee5\u5355\u4e0b\u5212\u7ebf\u5f00\u5934\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u6e05\u695a\u4f60\u7684\u4ee3\u7801\u4f1a\u6d89\u53ca\u5230\u5b50\u7c7b\uff0c\n\u5e76\u4e14\u6709\u4e9b\u5185\u90e8\u5c5e\u6027\u5e94\u8be5\u5728\u5b50\u7c7b\u4e2d\u9690\u85cf\u8d77\u6765\uff0c\u90a3\u4e48\u624d\u8003\u8651\u4f7f\u7528\u53cc\u4e0b\u5212\u7ebf\u65b9\u6848\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u65f6\u5019\u4f60\u5b9a\u4e49\u7684\u4e00\u4e2a\u53d8\u91cf\u548c\u67d0\u4e2a\u4fdd\u7559\u5173\u952e\u5b57\u51b2\u7a81\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\u5355\u4e0b\u5212\u7ebf\u4f5c\u4e3a\u540e\u7f00\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "lambda_ = 2.0 # Trailing _ to avoid clash with lambda keyword"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u6211\u4eec\u5e76\u4e0d\u4f7f\u7528\u5355\u4e0b\u5212\u7ebf\u524d\u7f00\u7684\u539f\u56e0\u662f\u5b83\u907f\u514d\u8bef\u89e3\u5b83\u7684\u4f7f\u7528\u521d\u8877\n(\u5982\u4f7f\u7528\u5355\u4e0b\u5212\u7ebf\u524d\u7f00\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u9632\u6b62\u547d\u540d\u51b2\u7a81\u800c\u4e0d\u662f\u6307\u660e\u8fd9\u4e2a\u5c5e\u6027\u662f\u79c1\u6709\u7684)\u3002\n\u901a\u8fc7\u4f7f\u7528\u5355\u4e0b\u5212\u7ebf\u540e\u7f00\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}